using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Threading;
using AvaloniaApplication1.Extensions;
using AvaloniaApplication1.ViewModels;

namespace AvaloniaApplication1;

public partial class PageHost : UserControl
{
    public PageHost()
    {
        InitializeComponent();
    }

    public static readonly StyledProperty<ApplicationPage> CurrentPageProperty =
        AvaloniaProperty.Register<PageHost, ApplicationPage>(nameof(CurrentPage), coerce: OnCallBack);

    public ApplicationPage CurrentPage
    {
        get => GetValue(CurrentPageProperty);
        set => SetValue(CurrentPageProperty, value);
    }

    public static readonly StyledProperty<ViewModelBase> CurrentPageViewModelProperty =
        AvaloniaProperty.Register<PageHost, ViewModelBase>(nameof(CurrentPageViewModel));

    public ViewModelBase CurrentPageViewModel
    {
        get => GetValue(CurrentPageViewModelProperty);
        set => SetValue(CurrentPageViewModelProperty, value);
    }

    private static ApplicationPage OnCallBack(AvaloniaObject sender, ApplicationPage currentPage)
    {
        var newPageFrame = (sender as PageHost);
        var currentPageViewModel = sender.GetValue(CurrentPageViewModelProperty);

        if (newPageFrame.Content is BasePage page &&
            page.ToApplicationPage() == currentPage)
        {
            // Just update the view model
            page.ViewModelObject = currentPageViewModel;
        
            return currentPage;
        }
        
        newPageFrame.NewPage.Content = currentPage.ToBasePage(currentPageViewModel);
        
        return currentPage;
        // Get the frames
        // var newPageFrame = (sender as PageHost).NewPage;
        // var oldPageFrame = (sender as PageHost).OldPage;
        //
        // // If the current page hasn't changed
        // // just update the view model
        // if (newPageFrame.Content is BasePage page &&
        //     page.ToApplicationPage() == currentPage)
        // {
        //     // Just update the view model
        //     page.ViewModelObject = currentPageViewModel;
        //
        //     return currentPage;
        // }
        //
        // // Store the current page content as the old page
        // var oldPageContent = newPageFrame.Content;
        //
        // // Remove current page from new page frame
        // newPageFrame.Content = null;
        //
        // // Move the previous page into the old page frame
        // oldPageFrame.Content = oldPageContent;
        //
        // // Animate out previous page when the Loaded event fires
        // // right after this call due to moving frames
        // if (oldPageContent is BasePage oldPage)
        // {
        //     // Tell old page to animate out
        //     oldPage.ShouldAnimateOut = true;
        //
        //     // Once it is done, remove it
        //     Task.Delay((int)(oldPage.SlideSeconds * 1000)).ContinueWith((t) =>
        //     {
        //         // Remove old page
        //         Dispatcher.UIThread.Invoke(() => oldPageFrame.Content = null);
        //     });
        // }
        //
        // // Set the new page content
        // newPageFrame.Content = currentPage.ToBasePage(currentPageViewModel);

        return currentPage;
    }
}